<template>
	<view class="RDMain">
		<view class="RDHead">
			<view class="RDHeadDetail">
				<view>{{listData.applyUnitName?listData.applyUnitName:'--'}}</view>
				<view>申请时间：{{listData.applyTime?(listData.applyTime+'').replace(/T/g," "):'--'}}</view>
				<view>退回资产总数：{{listData.applyQty+''}}</view>
				<view>已退回资产数：{{listData.execQty+''}}</view>
				<view>待退还资产数：{{listData.notExecQty+''}}</view>
			</view>

			<view class="assetListItem" v-for="(item,index) in items" :key="index">
				<view class="itemHead">
					<view class="itemHeadTitle">{{item.assetName?item.assetName:'--'}}</view>
					<view class="itemHeadState">
						{{item.statusCh?item.statusCh:'--'}}
					</view>
				</view>
				<view class="itemContent">
					<view class="conCenter">
						<view>资产类型：{{item.assetTypeName?item.assetTypeName:'--'}}</view>
						<view>资产编号：{{item.assetNo}}</view>
					</view>
				</view>
				<view class="itemHand">
					<view class="btn-s btn-green-s" v-if="item.statusCh=='处理中'"
						@click="execCon(item.assetExecDtlId,item.version)">确认</view>
					<view :class="{'btn-s':true,'btn-black-s':true,'permission-none':isPermission('ASS_APP_IMP')}"
						v-if="item.statusCh=='待处理'" @click="execBtn(item.assetExecDtlId,item.version,item.assetId)">实施
					</view>
				</view>
			</view>

			<view class="end">
				--到底了--
			</view>
		</view>
		<MessageTip ref="messageTip" :tipContent="tipContent" @clickType="handleShowOrHideTip" />
	</view>
</template>

<script lang="ts" setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		retrueDetail,
		exec,
		assetSign,
		getRetrueItems
	} from '@/common/api/assetAPI.ts'
	import {
		isPermission
	} from '@/common/util/common.ts'

	const listData = ref({})
	const items = ref([])
	const mainApplyId = ref('')
	const tipContent = ref('') //提示信息
	const messageTip = ref()

	function getList(mainApplyId) {
		retrueDetail(mainApplyId).then(value => {
			if (value.code == 0) {
				listData.value = value.data
			} else {
				uni.showToast({
					icon: 'none',
					title: '数据获取失败'
				})
			}
		})
	}

	function getItems(assetApplyId) {
		// if(!isPermission('ASS_IMP_DTL_QUERY')){
		getRetrueItems(assetApplyId).then(value => {
			if (value.code == 0) {
				items.value = value.data
				console.log(listData.value)
			} else {
				uni.showToast({
					icon: 'none',
					title: value.message
				})
			}
		})
		// }

	}

	onLoad((par) => {
		mainApplyId.value = par.mainApplyId
		getList(par.mainApplyId)
		getItems(par.mainApplyId)
	})
	// 确认
	const paramsConfirm = ref({})
	const execCon = (assetExecDtlId, version) => {
		tipContent.value = '确认签收吗？'
		messageTip.value.openTip() //打开弹窗
		paramsConfirm.value={assetExecDtlId,version}
	}
	
	const dialogConfirmSign = () => {
		assetSign([{
			assetExecDtlId:paramsConfirm.value.assetExecDtlId,
			version:paramsConfirm.value.version
		}]).then(value => {
			if (value.code == 0) {
				uni.showToast({
					icon: 'none',
					title: value.message,
					duration:500
				})
				setTimeout(() => {
					getItems(mainApplyId.value)
				}, 500)
			} else {
				uni.showToast({
					icon: 'none',
					title: value.message
				})
			}
		})
	}
	// 实施
	const paramsExec = ref({})
	const execBtn = (assetExecDtlId, version, assetId) => {
		tipContent.value = '确认实施吗？'
		messageTip.value.openTip() //打开弹窗
		paramsExec.value = {assetExecDtlId,version,assetId}

	}
	const dialogConfirmExec = () => {
		exec({
			assetExecDtlId:paramsExec.value.assetExecDtlId,
			version:paramsExec.value.version,
			assetId:paramsExec.value.assetId
		}).then(value => {
			if (value.code == 0) {
				uni.showToast({
					icon: 'none',
					title: value.message,
					duration:500
				})
				setTimeout(() => {
					getItems(mainApplyId.value)
				}, 500)
			} else {
				uni.showToast({
					icon: 'none',
					title: value.message
				})
			}
		})
	}
	const handleShowOrHideTip = (type:string)=>{
			if(type=='yes'){
				if(tipContent.value=='确认签收吗？'){
					dialogConfirmSign()
				}else{
					dialogConfirmExec()
				}
			}
		}
</script>

<style scoped lang="scss">
	.end {
		padding: 20px;
		text-align: center;
	}

	.RDMain {
		background-color: #eee;
	}

	.RDHead {
		padding: 15px;
	}

	.RDHead h2 {
		color: #333;
		margin-bottom: 15px;
	}

	.RDHeadDetail {
		border: 1px solid #ccc;
		box-sizing: border-box;
		padding: 15px;
		background-color: #fff;
	}

	.RDHeadDetail view:first-child {
		border-bottom: 1px solid #ccc;
		margin-bottom: 8px;
	}

	.RDHeadDetail view {
		padding-bottom: 8px;
		font-size: 14px;
	}

	.RDHeadDetail view:last-child {
		padding-bottom: 0;
	}

	.itemHead {
		position: relative;
	}

	.assetListItem {
		margin-top: 15px;
		background-color: #fff;
		border-radius: 5px;
	}

	.itemHeadTitle {
		text-indent: 15px;
		height: 45px;
		line-height: 45px;
		background-color: aliceblue;
		font-weight: bold;
		color: #333;
	}

	.itemHeadState {
		position: absolute;
		right: 0;
		top: 10px;
		display: inline-block;
		background-color: burlywood;
		font-size: 11px;
		color: #fff;
		padding: 5px 12px;
		border-radius: 15px 0 0 15px;
	}

	.itemContent {
		font-size: 15px;
		box-sizing: border-box;
		padding: 15px 10px;
		border-bottom: 1px solid #eee;
		display: flex;
		flex-wrap: row nowrap;
	}

	.itemContent>view:nth-child(1) {
		width: 85%;
		padding-left: 15px;
		color: #333;
	}

	.itemContent>view:nth-child(1)>view {
		padding: 3px 0;
		text-overflow: ellipsis;
		white-space: nowrap;
		overflow: hidden;
		cursor: pointer;
	}

	.itemContent>view:nth-child(2) {
		width: 15%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.itemHand {
		padding: 15px;
		display: flex;
		justify-content: flex-end;
	}

	.itemHand>view {
		padding: 4px 23px;
		margin-right: 14px;
	}

	.buttons {
		padding: 10px;
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-around;
		font-size: 15px;
	}

	// .buttons * {
	// 	font-size: 15px;
	// }
</style>
